<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文件上传</title>
<jsp:include page="/head.jsp" />
</head>
<body>
     <h1>文件上传</h1>
     <form action="demo/upload-1.let" enctype="multipart/form-data" method="post">
                           文件：<input required="required" type="file" name="img" accept="image/*"/>
          <button>提交</button> 
     </form>
     <br/>
     <h1>AJAX文件上传</h1>
     <form action="demo/upload-1.let" enctype="multipart/form-data" method="post">
                           文件：<input required="required" type="file" name="img" accept="image/*"/> 
          <button>提交</button> 
     </form>
     <script type="text/javascript">
          var form = document.forms[1];
          form.onsubmit = function(){
        	    let xhr = new XMLHttpRequest();
				xhr.onreadystatechange=function(){
					console.info(xhr.readyState+":"+xhr.status);
					if(xhr.readyState==4&&xhr.status==200){
						console.info(xhr.responseText);
						var data = JSON.parse(xhr.responseText);
						console.info(data);
						if(data.ok){
							form.reset();
							sys.toastr.success("上传成功：<a target='_blank' href='"+data.uri+"'>下载</a>");
						}else{
							sys.toastr.error("上传失败"+data.message);
						}
					}
				}
				xhr.open("post","demo/upload-1.let");
				xhr.send(new FormData(form));
        	    return false;
          }
     </script>
</body>
</html>